#include "http_server.h"
#include <memory>
#include "util.h"
const uint16_t g_default_port = 9999;

const string sep = "\r\n";

string HandlerHttp(string &message)
{
    // cout << "-------------------------------------" << endl;
    // cout << "请求信息开始：" << endl;
    // cout << message;
    // cout << "请求信息结束：" << endl;
    // cout << "-------------------------------------" << endl
    //      << endl;
    // string response = "HTTP/1.1 200 OK" + sep;
    // response += "Content-Length: 10";
    // response += sep;//添加标志
    // response += sep;//添加空行隔开请求报头和有效载荷
    // response += "hello http"; //需要加上正文长度content-length或者有标签<html><html>，否则浏览器会识别不了正文是否\
    // 读取完毕

    string body;
    Util::ReadFile(message, &body);

    string response = "HTTP/1.1 200 OK" + sep;
    response += "Content-Length: ";
    response += std::to_string(body.size()) + sep;
    response += "Content-Type: text/html" + sep;
    response += sep; // 添加标志
    // 正文
    response += body;
    return response;
}

int main(int args, char *argv[])
{
    uint16_t port = g_default_port;
    if (args == 2)
        port = atoi(argv[1]);
    std::unique_ptr<HttpServer> hs(new HttpServer(HandlerHttp, port));
    hs->InitServer();
    hs->StartServer();

    return 0;
}
